﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Text;
using JYStockAnalyser.Model;
using JYStockAnalyser.DA;

namespace JYStockAnalyser.Face
{
    /// <summary>
    /// 服务器缓存
    /// </summary>
    /// <remarks>一些频繁使用且不经常改动的数据，都通过此对象的单例获取，第一次使用这些数据时从数据库获取然后缓存到内存中，以后的获取将直接读取缓存中的数据</remarks>
    public class ServerCache
    {
        static ServerCache m_instance;
        
        public static ServerCache Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new ServerCache();
                }
                return m_instance;
            }
        }

        EstimateRule m_defualtRule;
        public EstimateRule DefaultEstimateRule
        {
            get
            {
                if (m_defualtRule == null)
                {
                    m_initEstimateRules();
                }
                return m_defualtRule;
            }
        }

        List<EstimateRule> m_estimateRules;

        public List<EstimateRule> EstimateRules
        {
            get
            {
                if (m_estimateRules == null)
                {
                    m_initEstimateRules();
                }
                return m_estimateRules;
            }
        }    

        private void m_initEstimateRules()
        {
            EstimateRuleMapper mapper = new EstimateRuleMapper();
            List<EstimateRule> rules = mapper.FindAllRules();

            foreach (EstimateRule rule in rules)
            {
                if (rule.IsDefault)
                {
                    m_defualtRule = rule;                    
                    break;
                }
            }

            m_estimateRules = new List<EstimateRule>();
            m_estimateRules.AddRange(rules);
            if (m_defualtRule != null)
            {
                m_estimateRules.Remove(m_defualtRule);
            }
        }
    }
}
